package com.ruoyi.project.controller;

import java.util.List;

import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工艺设计主表Controller
 * 
 * @author xiangjun
 * @date 2021-12-14
 */
@RestController
@RequestMapping("/gongyi/gy_main")
public class GyMainController extends BaseController
{
    @Autowired
    private IGyMainService gyMainService;
    @Autowired
    private IGyFormulaService gyFormulaService;
    @Autowired
    private IGyLoomingService gyLoomingService;
    @Autowired
    private IGyProcessService gyProcessService;
    @Autowired
    private IGyWarpingService gyWarpingService;

    /**
     * 查询工艺设计主表列表
     */
    @PreAuthorize("@ss.hasPermi('gongyi:gy_main:list')")
    @GetMapping("/list")
    public TableDataInfo list(GyMain gyMain)
    {
        startPage();
        List<GyMain> list = gyMainService.selectGyMainList(gyMain);
        return getDataTable(list);
    }

    /**
     * 导出工艺设计主表列表
     */
    @PreAuthorize("@ss.hasPermi('gongyi:gy_main:export')")
    @Log(title = "工艺设计主表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GyMain gyMain)
    {
        List<GyMain> list = gyMainService.selectGyMainList(gyMain);
        ExcelUtil<GyMain> util = new ExcelUtil<GyMain>(GyMain.class);
        return util.exportExcel(list, "工艺设计主表数据");
    }

    /**
     * 获取工艺设计主表详细信息
     */
    @PreAuthorize("@ss.hasPermi('gongyi:gy_main:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        GyMain gyMain = gyMainService.selectGyMainById(id);
        GyWarping gyWarping = gyWarpingService.selectGyWarpingById(gyMain.getWarpingId());
        GyFormula gyFormula = gyFormulaService.selectGyFormulaById(gyMain.getFormulaId());
        GyProcess gyProcess = gyProcessService.selectGyProcessById(gyMain.getProcessId());
        GyLooming gyLooming = gyLoomingService.selectGyLoomingById(gyMain.getLoomingId());
        GongyiForm result = new GongyiForm();
        result.setFormulaObj(gyFormula);
        result.setLoomingObj(gyLooming);
        result.setMainObj(gyMain);
        result.setWarpingObj(gyWarping);
        result.setProcessObj(gyProcess);

        return AjaxResult.success(result);
    }

    /**
     * 新增工艺设计主表
     */
    @PreAuthorize("@ss.hasPermi('gongyi:gy_main:add')")
    @Log(title = "工艺设计主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GongyiForm gongyiForm)
    {
        GyMain mid = gongyiForm.getMainObj();
        gyWarpingService.insertGyWarping(gongyiForm.getWarpingObj());
        mid.setWarpingId(gongyiForm.getWarpingObj().getId());
        gyLoomingService.insertGyLooming(gongyiForm.getLoomingObj());
        mid.setLoomingId(gongyiForm.getLoomingObj().getId());
        gyFormulaService.insertGyFormula(gongyiForm.getFormulaObj());
        mid.setFormulaId(gongyiForm.getFormulaObj().getId());
        gyProcessService.insertGyProcess(gongyiForm.getProcessObj());
        mid.setProcessId(gongyiForm.getProcessObj().getId());
        return toAjax(gyMainService.insertGyMain(mid));
    }

    /**
     * 修改工艺设计主表
     */
    @PreAuthorize("@ss.hasPermi('gongyi:gy_main:edit')")
    @Log(title = "工艺设计主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GongyiForm gongyiForm)
    {
        gyWarpingService.updateGyWarping(gongyiForm.getWarpingObj());
        gyLoomingService.updateGyLooming(gongyiForm.getLoomingObj());
        gyFormulaService.updateGyFormula(gongyiForm.getFormulaObj());
        gyProcessService.updateGyProcess(gongyiForm.getProcessObj());
        return toAjax(gyMainService.updateGyMain(gongyiForm.getMainObj()));
    }

    /**
     * 删除工艺设计主表
     */
    @PreAuthorize("@ss.hasPermi('gongyi:gy_main:remove')")
    @Log(title = "工艺设计主表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gyMainService.deleteGyMainByIds(ids));
    }
}
